package main

import (
	"alertmanagerWebhook/core"
	"alertmanagerWebhook/global"
	"alertmanagerWebhook/routers"
	"alertmanagerWebhook/utils"
	"net/http"
)

func main() {
	// 1. 先初始化配置(不使用logger)
	core.InitYaml()
	
	// 2. 初始化日志系统
	logger, cleanup, err := utils.SetupLogrus(
		global.Config.System.LogFileDir+global.Config.System.LogFilePath,
		global.Config.System.LogMaxSize,
		global.Config.System.LogMaxBackup,
		global.Config.System.LogMaxDay,
	)
	if err != nil {
		panic(err)
	}
	defer cleanup()
	global.Logger = logger
	
	// 3. 现在可以正常使用logger了
	global.Logger.Info("Configuration loaded successfully")

	router := routers.InitRouterGroup()

	srv := &http.Server{
		Addr:    global.Config.System.Addr(),
		Handler: router,
	}

	go func() {
		global.Logger.Info("Starting server...")
		if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			global.Logger.Fatalf("Listen: %s\n", err)
		}
	}()

	utils.HandleShutdown(srv)
}
